Options
enum Option<T> {
None,
Some(T),
}
let some_number = Some(5);
let some_char = Some('e');
let absent_number: Option<i32> = None;
as_mut
-
The
as_mutmethod is a method of optional types , likeOption<T>. -
It is used to convert an
Option<T>into anOption<&mut T>. This allows accessing the contained value mutably without moving the value.
let mut opt: Option<String> = Some(String::from("Hello"));
if let Some(value) = opt.as_mut() {
value.push_str(", world!"); // Modifies the contained value
}
println!("{:?}", opt); // Output: Some("Hello, world!")
Adapters for working with references
-
as_refconverts from&Option<T>toOption<&T>. -
as_mutconverts from&mut Option<T>toOption<&mut T>. -
as_derefconverts from&Option<T>toOption<&T::Target>. -
as_deref_mutconverts from&mut Option<T>toOption<&mut T::Target>. -
as_pin_refconverts fromPin<&Option<T>>toOption<Pin<&T>>. -
as_pin_mutconverts fromPin<&mut Option<T>>toOption<Pin<&mut T>>.
Using
?
-
Without
?:fn add_last_numbers(stack: &mut Vec<i32>) -> Option<i32> { let a = stack.pop(); let b = stack.pop(); match (a, b) { (Some(x), Some(y)) => Some(x + y), _ => None, } } -
With
?:fn add_last_numbers(stack: &mut Vec<i32>) -> Option<i32> { Some(stack.pop()? + stack.pop()?) }
Extracting the contained value
-
expectpanics with a provided custom message. -
unwrappanics with a generic message. -
unwrap_orreturns the provided default value. -
unwrap_or_defaultreturns the default value of the typeT(which must implement theDefaulttrait). -
unwrap_or_elsereturns the result of evaluating the provided function.
If let
let config_max = Some(3u8);
if let Some(max) = config_max {
println!("The maximum is configured to be {max}");
}
Matching
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);